﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using AEM.Controls;
using AEM.Data;
using Actions = AEM.Data.BLL.Actions;
using System.Windows.Controls;

namespace AEM
{
    /// <summary>
    /// GatherFire.xaml 的交互逻辑
    /// </summary>
    public partial class GatherFire : Window
    {
        public event EventHandler<EventArgs> HideEventHandler;
        private double maxwidth, maxheight;
        public List<FireModel> firelift = new List<FireModel>();

        public GatherFire()
        {
            InitializeComponent();
            this.sys_button_close.Click += new RoutedEventHandler(sys_button_close_Click);
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(TitleBar_MouseLeftButtonDown);
            this.Loaded += new RoutedEventHandler(GatherFire_Loaded);
            this.maxwidth = System.Windows.SystemParameters.WorkArea.Width - 50;
            this.maxheight = System.Windows.SystemParameters.WorkArea.Height - 50;

        }

        void GatherFire_Loaded(object sender, RoutedEventArgs e)
        {
            //LoadModels();
            //ResizeUI();
            //Actions.LiftInfo.LiftRunInGatherFire = doLiftRunInGatherFire;
        }

        private void LoadModels()
        {
            foreach (FireModel f in this.lift_group.Children)
            {
                this.UnregisterName(f.Name);
            }
            this.lift_group.Children.Clear();
            if (null != Config.LiftInftList)
            {
                foreach (var i in Config.LiftInftList)
                {
                    AddModel(i.LiftNum);
                }
                foreach (var i in this.lift_group.Children.OfType<FireModel>())
                {
                    i.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void AddModel(int liftnum)
        {
            FireModel m = new FireModel();
            m.Width = 220;
            m.Height = 110;
            m.LiftNum = liftnum;
            m.Name = "fm" + liftnum;
            this.RegisterName(m.Name, m);
            this.lift_group.Children.Add(m);
        }

        void doLiftRunInGatherFire(SerialDataEventArgs e)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                if (null == e || null == e.Data) return;
                FireModel fm = this.FindName("fm" + e.LiftNum) as FireModel;
                if (null != fm)
                {
                    if (e.Data.Fire)
                    {
                        fm.Visibility = Visibility.Visible;
                        fm.LiftRising = e.Data.Rising;
                        fm.LiftDescending = e.Data.Descending;
                        fm.DoorStatus = e.Data.DoorOpen;
                        fm.Floor = e.FloorShow;
                    }
                    else
                    {
                        fm.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {
                    AddModel(e.LiftNum);
                    //doLiftRunInGatherFire(e);
                }
                //ResizeUI();
            }));
        }

        private void ResizeUI()
        {
            int count = (from f in this.lift_group.Children.OfType<FireModel>()
                         where f.Visibility == Visibility.Visible
                         select f).Count();
            if (count < 1) sys_button_close_Click(null, null);
            count = count <= 0 ? 1 : count;
            double sq = Math.Sqrt(count);
            int w = (int)Math.Ceiling(sq);
            int h = (int)Math.Ceiling(sq);
            double nw = w * 220 + 100;
            double nh = h * 130 + 150;
            this.Width = this.MinWidth = this.MaxWidth = nw > this.maxwidth ? this.maxwidth : nw;
            this.Height = this.MinHeight = this.MaxHeight = nh > this.maxheight ? this.maxheight : nh;
            //this.TitleBar.Width = this.Width - 100;

            this.Left = (System.Windows.SystemParameters.WorkArea.Width - this.Width) / 2;
            this.Top = (System.Windows.SystemParameters.WorkArea.Height - this.Height) / 2;
        }

        void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            //foreach (var i in this.lift_group.Children.OfType<FireModel>())
            //{
            //    i.Visibility = Visibility.Collapsed;
            //}
            if (null != HideEventHandler)
                HideEventHandler.Invoke(this, null);
            //this.Close();
        }

    }
}
